#!/usr/bin/python
# -*- coding=utf8 -*-
"""
Copyright (C) 2010 by lwp
levin108@gmail.com

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the
Free Software Foundation, Inc.,
51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
"""

import sys
import oauth
import os, re, time
import types
import twitter
from pdfgen import tp_document
import pdfgen
import oauthapi
import config

def get_tweets_type():
	print 'Please choose the type of tweets you want to generate:'
	print '[1] timeline of yourself'
	print '[2] my tweets retweeted'
	print '[3] tweets retweeted by me'
	print '[4] timeline of somebody'

	try:
		num = int(raw_input('Input the number:'))
		if num < 1 or num > 4:
			return get_tweets_type()
		else:
			return num
	except ValueError:
		return get_tweets_type()

def get_tweets_count():
	count = raw_input('Please input tweets count(default for all):')
	if len(count) == 0:
		count = int(0)
	
	try:
		count = int(count)
		return count
	except ValueError:
		return get_tweets_count()

def get_max_id():
	tweetid = raw_input('Please input max tweets id(enter to ignore):')
	try:
		return long(tweetid)
	except:
		return 0

def get_min_id():
	tweetid = raw_input('Please input min tweets id(enter to ignore):')
	try:
		return long(tweetid)
	except:
		return 0

def get_font_path():
	font_path = raw_input('Please input the location of the font file,eg. /usr/share/fonts/truetype/msyh.ttf:')
	if len(font_path) == 0:
		return get_font_path()
	if not os.path.exists(font_path):
		print '%s doesn\'t exist' % font_path
		return get_font_path()
	return font_path

def get_link_color():
	link_color = raw_input('Please input the link color[blue]:')
	if len(link_color) == 0:
		return 'blue'
	else:
		return link_color

def get_output_name(tweets_type = {}, screen_name = {}):
	if tweets_type == pdfgen.TWEETS_MY_TIMELINE or tweets_type == pdfgen.TWEETS_OTHERS_TIMELINE:
		default_name = 'tweets_%s.pdf' % screen_name
	elif tweets_type == pdfgen.TWEETS_RETWEETED_BY_ME:
		default_name = 'tweets_%s_rt.pdf' % screen_name
	elif tweets_type == pdfgen.TWEETS_MY_RETWEETED:
		default_name = 'tweets_%s_rted.pdf' % screen_name

	filename = raw_input('Please input the output filename[%s]:' % default_name)
	if len(filename) == 0:
		filename = default_name

	return filename
	
def main():
	config.check_system()

	cfg = config.tp_cfg()
	if not cfg.load():
		print 'This is the first time you use tweets2pdf,needs a little initialization'
		fontpath = get_font_path()
		linkcolor = get_link_color()
		cfg.save(font_path = fontpath, link_color = linkcolor)

	client = oauthapi.tpOAuth()

	if not client.restore_access_token():
		token = client.fetch_request_token()

		client.authorization_token(token)

		verifier = raw_input("please input PIN code:")
		token = client.fetch_access_token(token, verifier)
		client.save_access_token()

	tweets_type = get_tweets_type()

	if tweets_type == pdfgen.TWEETS_OTHERS_TIMELINE:
		screen_name = raw_input('Please input screen name:')
	else:
		screen_name = client.access_token.screen_name

	pdf_name = get_output_name(tweets_type, screen_name)
	
	tweets_max_id = get_max_id()
	tweets_min_id = get_min_id()

	tpdoc = tp_document(oauth_client = client, \
			screen_name = screen_name, \
			tweets_type = tweets_type, \
			pdf_name = pdf_name, \
			tweets_min_id = tweets_min_id, \
			tweets_max_id = tweets_max_id, \
			font_path = cfg.font_path, \
			link_color = cfg.link_color)

	tpdoc.set_tweets_count(get_tweets_count())

	tpdoc.generate_cover()
	tpdoc.generate_body()

	tpdoc.dump()

if __name__ == '__main__':
	main()
